﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Beryl.OrderControllers
{
    public class MainOrderQuery
    {
        private static MainOrderQuery instance = null;
        private static readonly object uniqueLock = new object();

        public static MainOrderQuery Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new MainOrderQuery();
                    }
                }
                return instance;
            }
        }
        public MainOrderQuery() { }

        //查询订单类别
        public List<string> QueryOrderType(string para)
        {
            List<string> list = new List<string>();
            try
            {
                string sql = string.Format(@"select distinct MotypeCode from APS_Motype WHERE cProductDepNo = '{0}';", para);
                string ret = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));
                if (ret.Equals("[]"))
                {
                    throw new Exception("查询数据为空");
                }
                list.Add(ret);
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //查询订单类别名字
        public List<string> MainODescription(string para)
        {
            List<string> list = new List<string>();
            try
            {
                string sql = string.Format(@"select distinct Description FROM APS_Motype where MotypeCode = '{0}';", para);
                string ret = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));
                if (ret.Equals("[]"))
                {
                    throw new Exception("查询数据为空");
                }
                list.Add(ret);
            }
            catch (Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //更新主计划
        public List<string> UpdateMainOrder(string para)
        {
            Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            List<string> list = new List<string>();
            try
            {
                var k = jArray[0];
                string MotypeCode = k["MotypeCode"].ToString();
                string Description = k["Description"].ToString();
                string cPOrderNo = k["cPOrderNo"].ToString();
                string cCusName = k["cCusName"].ToString();
                var sql = string.Format(@"update APS_PlanOrder set cCusName = '{0}',MotypeCode = '{1}',Description = '{2}' where cPOrderNo = '{3}';", cCusName, MotypeCode, Description, cPOrderNo);
                List<string> sqlArr = new List<string>
                {
                    sql
                };
                bool ok = CommonMethod.Instance.insertSqlList(sqlArr);
                if (!ok)
                {
                    throw new Exception("插入失败");
                }
                list.Add("success");
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //关闭订单
        public List<string> CloseMOrder(string para)
        {
            Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            List<string> list = new List<string>();
            try
            {
                var k = jArray[0];
                string remarks = k["remarks"].ToString();
                string closePerson = k["closePerson"].ToString();
                string cPOrderNo = k["cPOrderNo"].ToString();
                string cProductDepNo = k["cProductDepNo"].ToString();
                string[] strings = cPOrderNo.Split(',');
                var sql = "";
                var sql1 = "";
                List<string> sqlAll = new List<string>();
                for (int i = 0; i < strings.Length; i++)
                {
                    //更新生产订单 状态为0091==>已关闭
                    sql += string.Format(@"update APS_PlanOrder set cStatusNo = '0091',remarks = '{0}',closeTime = '{1}',closePerson = '{2}' where cPOrderNo = '{3}' and cProductDepNo = '{4}';", remarks, CommonMethod.Instance.getTimes(), closePerson, strings[i],cProductDepNo);

                    //更新销售订单 状态为0000
                    var sqlQuery = string.Format(@"select cSourceNo as cSONo from APS_PlanOandSaleORelat where cPOrderNo = '{0}' and cProductDepNo = '{1}';", strings[i],cProductDepNo);
                    string ret = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sqlQuery));
                    if(!ret.Equals("[]"))
                    {
                        Newtonsoft.Json.Linq.JArray jArray1 = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(ret);
                        var k1 = jArray1[0];
                        var cSONo = k1["cSONo"].ToString(); //子销售订单号
                        sql += string.Format(@"update APS_SOrder set cStatusNo = '0000' where cSONo = '{0}' and cStatusNo < '0050';",cSONo);
                    }
                    //删除生产订单与销售订单关系表数据
                    sql1 += string.Format(@"delete from APS_PlanOandSaleORelat where cPOrderNo = '{0}' and cProductDepNo = '{1}';", strings[i], cProductDepNo);
                    sqlAll.Add(sql);
                    sqlAll.Add(sql1);
                }
                bool ok = CommonMethod.Instance.insertSqlList(sqlAll);
                if (!ok)
                {
                    throw new Exception("插入失败");
                }
                list.Add("success");
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }
    }
}